//
//  SLRegisterViewController.m
//  SoccerLottery
//
//  Created by chuanshuangzhang chuan shuang on 15/12/24.
//  Copyright © 2015年 chuanshuangzhang. All rights reserved.
//

#import "SLRegisterViewController.h"

@interface SLRegisterViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UITextField *phoneTextField;
@property (nonatomic,strong) UITextField *inputCodeField;
@property (nonatomic,strong) UITextField *codeTextField;
@property (nonatomic,strong) UITextField *confirmCodeTextField;
@property (nonatomic,strong) UIButton *codeBtn;
@end

@implementation SLRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"会员注册" back:YES];
    [self addKeyboardButton];
    [self registerKeyboardNotification];
    
    //设置账号输入框
    _phoneTextField = [[UITextField alloc]init];
    _phoneTextField.frame = CGRectMake(15, 50, App_Frame_Width-30, 44);
    _phoneTextField.delegate = self;
    _phoneTextField.keyboardType = UIKeyboardTypePhonePad;
    [_phoneTextField setPlaceholder:@"请输入手机号码"];
    [_phoneTextField applyInputStyle];
    [self.view addSubview:_phoneTextField];
    
    //设置密码输入框
    _inputCodeField = [[UITextField alloc]init];
    _inputCodeField.frame = CGRectMake(15, _phoneTextField.bottom+17, App_Frame_Width/2.0-15, 44);
    _inputCodeField.delegate = self;
    _inputCodeField.keyboardType = UIKeyboardTypeNumberPad;
    [_inputCodeField setPlaceholder:@"请输入验证码"];
    [_inputCodeField applyInputStyle];
    [self.view addSubview:_inputCodeField];
    
    //设置密码输入框
    _codeTextField = [[UITextField alloc]init];
    _codeTextField.frame = CGRectMake(15, _inputCodeField.bottom+17, App_Frame_Width-30, 44);
    _codeTextField.delegate = self;
    _codeTextField.keyboardType = UIKeyboardTypeASCIICapable;
    _codeTextField.secureTextEntry = YES;
    [_codeTextField setPlaceholder:@"设置密码"];
    [_codeTextField applyInputStyle];
    [self.view addSubview:_codeTextField];
    
    //设置密码输入框
    _confirmCodeTextField = [[UITextField alloc]init];
    _confirmCodeTextField.frame = CGRectMake(15, _codeTextField.bottom+17, App_Frame_Width-30, 44);
    _confirmCodeTextField.delegate = self;
    _confirmCodeTextField.keyboardType = UIKeyboardTypeASCIICapable;
    _confirmCodeTextField.secureTextEntry = YES;
    [_confirmCodeTextField setPlaceholder:@"确认密码"];
    [_confirmCodeTextField applyInputStyle];
    [self.view addSubview:_confirmCodeTextField];
    
    //设置获取验证码按钮
    
    _codeBtn = [[UIButton alloc]init];
    [_codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    _codeBtn.frame = CGRectMake(_inputCodeField.right + 20, _phoneTextField.bottom+17, App_Frame_Width-35-_inputCodeField.right, 44);
    [_codeBtn applyActionStyle];
    [_codeBtn setBackgroundImage:[Utils createImageWithColor:RGBACOLOR(230, 51, 76, 1.0)] forState:UIControlStateNormal];
    [_codeBtn addTarget:self action:@selector(getVerifyCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_codeBtn];
    
    
    UIButton *registerBtn = [[UIButton alloc]init];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    registerBtn.frame = CGRectMake(25, _confirmCodeTextField.bottom + 40, App_Frame_Width-50, 44);
    [registerBtn applyActionStyle];
    [registerBtn addTarget:self action:@selector(registerUser:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
    // Do any additional setup after loading the view.
}

- (BOOL)checkInfo
{
    if(_phoneTextField.text.length != 11){
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return NO;
    }else if(_inputCodeField.text.length != 6){
        [SVProgressHUD showErrorWithStatus:@"请输入正确的验证码"];
        return NO;
    }else if(_codeTextField.text <=0){
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return NO;
    }else if(_confirmCodeTextField.text.length <= 0){
        [SVProgressHUD showErrorWithStatus:@"请输入确认密码"];
        return NO;
    }else if(![_confirmCodeTextField.text isEqualToString:_codeTextField.text]){
        [SVProgressHUD showErrorWithStatus:@"确认密码和输入密码不一致"];
        return NO;
    }
    return YES;
}

- (IBAction)getVerifyCode:(id)sender
{
    [self resignKeyboard:nil];
    if(self.phoneTextField.text.length != 11){
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码"];
        [self.phoneTextField becomeFirstResponder];
    }else {
        if(![VWebService isConnected]){
            return [SVProgressHUD showErrorWithStatus:@"无网络链接，请检查您的网络"];
        }
        self.codeBtn.userInteractionEnabled = NO;
        //调用接口获得手机验证码
        [SVProgressHUD showWithStatus:@"正在获取验证码"];
        [SLUserNetMgr getVerfiyCode:self.phoneTextField.text type:@"1" block:^(id result, BOOL status, NSError *error) {
            if(NET_SUCCESS(result, status)){
                [SVProgressHUD dismiss];
                [Utils ShowTimeDownWithTime:60 block:^(dispatch_source_t timer, BOOL finish, NSInteger time) {
                    if(finish){
                        [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                        self.codeBtn.userInteractionEnabled = YES;
                    }else {
                        [self.codeBtn setTitle:[NSString stringWithFormat:@"倒计时%li秒",(long)time] forState:UIControlStateNormal];
                    }
                }];
            }else {
                self.codeBtn.userInteractionEnabled = YES;
                [SVProgressHUD showErrorWithStatus:ERRORMSG(result)];
            }
        }];
    }
}
- (IBAction)registerUser:(id)sender
{
    [self resignKeyboard:nil];
    if(![self checkInfo]){
        return;
    }
    if(![VWebService isConnected]){
        return [SVProgressHUD showErrorWithStatus:@"无网络链接，请检查您的网络"];
    }
    __weak SLRegisterViewController *weakSelf = self;
    [SVProgressHUD showWithStatus:@"正在注册"];
    [SLUserNetMgr registerUser:_phoneTextField.text password:_codeTextField.text userName:@"" code:_inputCodeField.text block:^(id result, BOOL status, NSError *error) {
        if(NET_SUCCESS(result, status)){
            [SVProgressHUD showSuccessWithStatus:@"注册成功，请登录"];
            if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didUserRegisterSucced:)]){
                [weakSelf.delegate didUserRegisterSucced:weakSelf];
            }
        }else {
            [SVProgressHUD showErrorWithStatus:ERRORMSG(result)];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
